using Newtonsoft.Json;
using System;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
namespace Lyh.Train.Common.Convertor
{
	public static class JsonHelper
	{
		public static string ToJson(object obj)
		{
			return JsonConvert.SerializeObject(obj);
		}
		public static T FromJson<T>(string jsonStr)
		{
			return JsonConvert.DeserializeObject<T>(jsonStr);
		}
		public static T JsonDeserialize<T>(string json)
		{
			T result;
			try
			{
				DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
				MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(json.ToCharArray()));
				T t = (T)((object)dataContractJsonSerializer.ReadObject(memoryStream));
				memoryStream.Close();
				result = t;
			}
			catch
			{
				result = default(T);
			}
			return result;
		}
	}
}
